//
//  NetWorkingManager.m
//  ShaoPingWuLiu
//
//  Created by WeiPan on 15/11/23.
//  Copyright (c) 2015年 WPJoe. All rights reserved.
//

#import "NetWorkingManager.h"
#import "URL.h"

@implementation NetWorkingManager

+ (AFHTTPRequestOperation *)postWithUrl:(NSString *)urlString requestParams:(NSDictionary *)params SuccessHandler:(SuccessBlock)success failureHandler:(FailureBlock)failure {
    NSString * url = [BASIC_URL stringByAppendingString:urlString];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@", params);
    //配置头部参数
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    [manager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
////    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%ld", (unsigned long)[jsonStr length]] forHTTPHeaderField:@"Content-Length"];
//    [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
//    [manager.requestSerializer setValue:[UserModel defaultUser].token ? [UserModel defaultUser].token : [[NSUserDefaults standardUserDefaults]stringForKey:@"token"] forHTTPHeaderField:@"token"];
    /**< 设置超时请求时间 */
//    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
//    manager.requestSerializer.timeoutInterval = 10.f;
//    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    AFHTTPRequestOperation *operation = [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) { success(responseObject); }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) { failure(error); }
    }];
    return operation;
}
+ (NSDictionary *)paramsByAppendingUserInfo:(NSDictionary *)params {
    NSMutableDictionary * newParams = [NSMutableDictionary dictionaryWithDictionary:params];
    newParams[@"token"] = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"token"]];
    return newParams;
}
+ (void)cancelAllOperations {
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.operationQueue cancelAllOperations];
}

@end
